﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient; 

namespace LOGIN
{
    public partial class Replenishment : Form
    {
        public Replenishment()
        {
            InitializeComponent();
            this.dgvMedical.AllowUserToAddRows = false;
        }
       

        
        private void Replenishmentupdata_Click(object sender, EventArgs e)
        {
            SqlConnection sqlConnection = new SqlConnection();                                              
            sqlConnection.ConnectionString =
                "Server=(local);Database=MyHospital;Integrated Security=sspi";                        
            SqlCommand updateCommand = new SqlCommand();                                                    
            updateCommand.Connection = sqlConnection;                                                     
            updateCommand.CommandText =                                                                    
                "UPDATE medicineData"
                + " SET DrugName=@DrugName,Guige=@Guige,DW=@DW,MedicineSum=@MedicineSum,IndPrice=@IndPrice,MinNum=@MinNum"
                + " WHERE MedicineID=@MedicineID;";
            updateCommand.Parameters.Add("@MedicineID", SqlDbType.Char, 0, "MedicineID");
            updateCommand.Parameters.Add("@DrugName", SqlDbType.VarChar, 0, "DrugName");                                 
            updateCommand.Parameters.Add("@Guige", SqlDbType.VarChar, 0, "Guige");
            updateCommand.Parameters.Add("@DW", SqlDbType.VarChar, 0, "DW");
            updateCommand.Parameters.Add("@MedicineSum", SqlDbType.Char, 0, "MedicineSum");
            updateCommand.Parameters.Add("@IndPrice", SqlDbType.Char, 0, "IndPrice");
            updateCommand.Parameters.Add("@MinNum", SqlDbType.Char, 0, "MinNum");
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();                                                                              
            sqlDataAdapter.UpdateCommand = updateCommand;                                                                                               
            DataTable medicalTable1 = (DataTable)this.dgvMedical.DataSource;                             
            sqlConnection.Open();                                                                          
            int rowAffected = sqlDataAdapter.Update(medicalTable1);                                         
            sqlConnection.Close();                                                                        
            MessageBox.Show("更新" + rowAffected.ToString() + "行。");  
        }

        private void Replenishment_Load(object sender, EventArgs e)
        {

            name.Text = ordersHelper.UserName;
            datatime.Text = DateTime.Now.ToLongDateString().ToString();
            SqlConnection sqlConnection = new SqlConnection();                                           
            sqlConnection.ConnectionString =
                "Server=(local);Database=MyHospital;Integrated Security=sspi";                
            SqlCommand sqlCommand = new SqlCommand();                                                      
            sqlCommand.Connection = sqlConnection;                                                          
            sqlCommand.CommandText = "SELECT MedicineID,YKNo,DrugName,Guige,DW,MedicineSum,IndPrice,MinNum FROM medicineData;";                              
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();                                       
            sqlDataAdapter.SelectCommand = sqlCommand;                                                     
            DataTable studentTable = new DataTable();                                                       
            sqlConnection.Open();                                                                           
            sqlDataAdapter.Fill(studentTable);                                                              
            sqlConnection.Close();                                                                          
            this.dgvMedical.DataSource = studentTable;
            dgvMedical.Columns[0].ReadOnly = true;
            dgvMedical.Columns[1].ReadOnly = true;
            dgvMedical.Columns[0].HeaderCell.Value = "药品ID";
            dgvMedical.Columns[1].HeaderCell.Value = "药库ID";
            dgvMedical.Columns[2].HeaderCell.Value = "药品名";
            dgvMedical.Columns[3].HeaderCell.Value = "规格";
            dgvMedical.Columns[4].HeaderCell.Value = "单位";
            dgvMedical.Columns[5].HeaderCell.Value = "库存量";
            dgvMedical.Columns[6].HeaderCell.Value = "入库价格";
            dgvMedical.Columns[7].HeaderCell.Value = "警告数量";

        }

        private void ReplenishmentToview_Click(object sender, EventArgs e)
        {

            SqlConnection sqlConnection = new SqlConnection();                                              
            sqlConnection.ConnectionString =
                "Server=(local);Database=MyHospital;Integrated Security=sspi";                             
            SqlCommand sqlCommand = new SqlCommand();                                                      
            sqlCommand.Connection = sqlConnection;
            if (this.comboBox1.SelectedIndex == 0 && this.search.Text != "")
            {
                sqlCommand.CommandText = "SELECT MedicineID,YKNo,DrugName,Guige,DW,MedicineSum,IndPrice,MinNum FROM medicineData where MedicineID=@MedicineID;";                                          
            sqlCommand.Parameters.AddWithValue("@MedicineID", this.search.Text.Trim());
            }
            else if (this.comboBox1.SelectedIndex == 1 && this.search.Text != "")
            {
                sqlCommand.CommandText = "SELECT MedicineID,YKNo,DrugName,Guige,DW,MedicineSum,IndPrice,MinNum FROM medicineData where DrugName=@DrugName;";                                      
                sqlCommand.Parameters.AddWithValue("@DrugName", this.search.Text.Trim());
            }
            else if ((this.comboBox1.SelectedIndex == 0 || this.comboBox1.SelectedIndex == 1) && this.search.Text == "")
            {
                sqlCommand.CommandText = "SELECT MedicineID,YKNo,DrugName,Guige,DW,MedicineSum,IndPrice,MinNum FROM medicineData ;";                                        
            }
           
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();                                           
            sqlDataAdapter.SelectCommand = sqlCommand;                                                      
            DataTable medicalTable = new DataTable();                                                 
            sqlConnection.Open();                                                                         
            sqlDataAdapter.Fill(medicalTable);                                                             
            sqlConnection.Close();                                                                          
            this.dgvMedical.DataSource = medicalTable;
            dgvMedical.Columns[0].ReadOnly = true;
            dgvMedical.Columns[1].ReadOnly = true;
        }  
        
        }
    }

